    # -*- encoding: utf-8 -*-
##############################################################################
#
#    OpenERP, Open Source Management Solution
#    Copyright (C) 2004-2009 Tiny SPRL (<http://tiny.be>). All Rights Reserved
#    $Id$
#
#    This program is free software: you can redistribute it and/or modify
#    it under the terms of the GNU General Public License as published by
#    the Free Software Foundation, either version 3 of the License, or
#    (at your option) any later version.
#
#    This program is distributed in the ho   pe that it will be useful,
#    but WITHOUT ANY WARRANTY; without even the implied warranty of
#    MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
#    GNU General Public License for more details.
#
#    You should have received a copy of the GNU General Public License
#    along with this program.  If not, see <http://www.gnu.org/licenses/>.
#
##############################################################################
import time
import netsvc

from osv import fields, osv

from tools.misc import currency
from tools.translate import _
import pooler

from tools import config


class kdvn_po_payment_term_line(osv.osv):
    _name = "kdvn.po.payment.term.line"
    _description = "KDVN P.O. Payment Term Line"
    #_inherit="kdvn.payment.term.line"
    
    def _new_seq(self, cr, uid, context={}):
        a=0
        if context:
            if 'order_id' in context:
                #payment_line_ids=self.search(cr,uid,[('order_id','=',context['order_id'])])
                if context['order_id']:
                    cr.execute('Select coalesce(max(sequence),0) from kdvn_po_payment_term_line where order_id = %s' % context['order_id'])
                    sq = cr.fetchone()
                    if sq:
                        a=sq[0]
                    a=a+5  
        return a
    
    def _new_amount(self, cr, uid, context={}):
        amount=0.0
        if context:
            if 'order_id' in context:
                order_id = context['order_id']
                if order_id:
                    cr.execute('Select type,value_amount from kdvn_po_payment_term_line where order_id = %s' % order_id)
                    #for po in self.pool.get('purchase.order').browse(cr,uid,[order_id],context):
                    amount = 100
                    for tpe,valuea in cr.fetchall():
                        if tpe=='p':
                            amount = amount - valuea
        return amount

    def name_get(self, cr, uid, ids, context={}): # Return name + " - " + type
        if not len(ids):
            return []
        reads = self.read(cr, uid, ids, ['name', 'type'], context)
        res = []
        for record in reads:
            payment_term_type=self.fields_get(cr,uid,fields='type')
            name = dict(payment_term_type['type']['selection'])[record['type']]            
            if record['name'] and name:
                name = record['name'] + ' - ' + name
            res.append((record['id'], name))
        return res
    
    _columns = {
        'name': fields.char('Description', size=100, required=True),
        'sequence': fields.integer('Sequence',required=True, help="The sequence field is used to order the payment term lines from the lowest sequences to the higher ones"),
        'type':fields.selection([('adv','Advanced'),('p','Progress'),('re','Retention')],'Type',required=True),
        'value_amount':fields.float('Percentage'),        
        'order_id':fields.many2one('purchase.order','Orders'),
        'due_date':fields.date('Due date'),
    }
    _sql_constraints = [('value_amount_greater_0', 'check(value_amount>0 and value_amount<=100)', 'KDVN Error: The amount must be between 1 and 100!')]
    _defaults = {
        'name': lambda *a:'Progress',
        'sequence': _new_seq,
        'type':lambda *x:'p',
        'value_amount':_new_amount
    }
    _order = "sequence"
kdvn_po_payment_term_line()
